package com.sstir.doctor.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sstir.doctor.es.doc.DoctorDoc;
import com.sstir.doctor.es.repository.DoctorRepository;
import com.sstir.doctor.entity.IndexListEntity;
import com.sstir.doctor.mapper.IndexListMapper;
import com.sstir.doctor.service.IIndexListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2021-01-15
 */
@Service
@Slf4j
public class IndexListServiceImpl extends ServiceImpl<IndexListMapper, IndexListEntity> implements IIndexListService {

    @Autowired
    private DoctorRepository doctorRepository;

    /**
     * 查-插
     */
    @Override
    public void importDataToEs() {
        log.info("*****************开始同步数据到ES******************************");
        int pageNo = 1;
        int pageSize = 4000;
        do {
            log.info("执行第{}页查询",pageNo);
            IPage<IndexListEntity> page = new Page<>(pageNo, pageSize);
            List<DoctorDoc> doctorDocList = super.page(page).convert(entity -> {
                DoctorDoc doctorDoc = new DoctorDoc();
                doctorDoc.setId(UUID.randomUUID().toString());
                doctorDoc.setKey(entity.getIndexKey());
                doctorDoc.setType(entity.getIndexType());
                doctorDoc.setValue(entity.getIndexValue());
                return doctorDoc;
            }).getRecords();
            doctorRepository.saveAll(doctorDocList);
            pageSize = doctorDocList.size();
            pageNo++;
        }while (pageSize == 4000);
            log.info("*****************数据同步ES完成******************************");
    }
}
